package com.miplus.ccrm.core.service.crd;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseService;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.R;
import com.miplus.ccrm.core.controller.vo.credit.LoanInvoiceListReqVo;
import com.miplus.ccrm.core.controller.vo.credit.LoanInvoiceListResVo;
import com.miplus.ccrm.core.pojo.IouLoanInvoiceInfo;
import com.miplus.ccrm.core.pojo.IouRepayPlan;
import com.miplus.ccrm.core.service.crd.IouLoanInvoiceInfoService;
import com.miplus.ccrm.core.mapper.IouLoanInvoiceInfoMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author wangzl
* @description 针对表【iou_loan_invoice_info(借据表(业务表))】的数据库操作Service实现
* @createDate 2024-11-07 11:23:24
*/
@Service
public class IouLoanInvoiceInfoServiceImpl extends MPJBaseServiceImpl<IouLoanInvoiceInfoMapper, IouLoanInvoiceInfo>
    implements IouLoanInvoiceInfoService{
    @Override
    public IPage<LoanInvoiceListResVo> loanInvoiceList(LoanInvoiceListReqVo paras)
    {
//        return selectJoinListPage(new Page<>(paras.getPageNum(),paras.getPageSize()),
//                LoanInvoiceListResVo.class,
//                new MPJLambdaWrapper<IouLoanInvoiceInfo>()
//                        .selectAll(IouLoanInvoiceInfo.class)
//                        .leftJoin(IouRepayPlan.class,"over",on->
//                            on.eq(IouRepayPlan::getLoanInvoiceId,IouLoanInvoiceInfo::getLoanInvoiceId)
//                                    .gt(IouRepayPlan::getOverdueDays,0)
//                        )
//                        .leftJoin(IouRepayPlan.class,"remaining",IouRepayPlan::getLoanInvoiceId,IouLoanInvoiceInfo::getLoanInvoiceId)
//                        .selectAs("sum(over.left_repay_principal)",LoanInvoiceListResVo::getOverduePrincipal)
//                        .selectAs("sum(over.left_repay_interest)",LoanInvoiceListResVo::getOverdueInterest)
//                        .selectAs("sum(over.left_repay_fee)",LoanInvoiceListResVo::getOverdueFees)
//                        .selectAs("sum(over.left_overdue_fee)",LoanInvoiceListResVo::getDefaultInterest)
//                        .selectAs("sum(remaining.left_repay_principal)",LoanInvoiceListResVo::getRemainingTotalAmount)
//                        .eq(ObjectUtils.isNotNull(paras.getCreditcaseid()),IouLoanInvoiceInfo::getCreditcaseid,paras.getCreditcaseid())
//                );

        return baseMapper.LoanInvoicePage(new Page<>(paras.getPageNum(),paras.getPageSize()),
                paras.getCreditcaseid(),
                paras.getLoanInvoiceStatus());
    }

    @Override
    public R<String> getInvoiceGrp(String creditcaseid) {
        String msg=null;
        List<String> invoiceGrp = baseMapper.getInvoiceGrp(Long.valueOf(creditcaseid));
        if(invoiceGrp!=null && invoiceGrp.size()>=2){
            msg="ALL";
        }else {
            msg=invoiceGrp.get(0);
        }
        return R.Success(msg);
    }
}




